#' ---
#' author: "```matthew.pietryka@gmail.com```"
#' date: "2019-05-13"
#' output:
#'  html_document:
#'    warning: false
#'    toc: true
#'    code_folding: hide
#' ---


#' **PURPOSE:**
#' This script creates plots to display the most influential constitutions (Figure A1)"




#  load packages  ----------------
library(tidyverse)   # DATA CLEANING FUNCTIONS
library(stringr)     # STRING FUNCTIONS
library(ggrepel)
library(lubridate)   # DATE/TIME FUNCTIONS

#  load data ----------------

# 'dyads_df' OBJECT DATA CREATED IN '1-clean-the-data/SC-1- Dyadic Data.R'
dyads_df <- read_rds("../Data/Derived/dyads_df.rds")
attr(dyads_df, "source")

# SOURCE (source): INNOVATIVE (new)
# FOCAL (source_to):    FULL TEXT
new_to_full_df <- read_rds("../Data/Derived/new_to_full_df.rds")


source("SC-Plots-Preferences.R")



# CREATE PLOTTING DATA  ------



allyears_df <- new_to_full_df %>%
  left_join(dyads_df)   %>%
  # FOCUS ON RELEVANT YEARS
  filter(date_to > date_from) %>%
  # EXCLUDE DYADS FROM SAME STATE
  filter(state_code_to != state_code_from)  %>%
  # POST-1776
  filter(year_to >= 1776)  %>%
  # TOTAL BORROWED FROM OTHER STATES
  group_by(label_to)  %>%
  mutate(to_ratio_sum = sum(ratio))  %>%
  ungroup()  %>%
  # STATE LABELS
  mutate(from_lab = paste(year_from, state_full_from),
         to_lab = paste(year_to, state_full_to))  %>%
  select(state_code_to, state_code_from,
         from_lab, date_from, to_lab, date_to, ratio, to_ratio_sum)


# BEFORE CIVIL WAR
prewar_df <- allyears_df %>%
  filter(date_to < ymd("1861-04-12")) %>%
  filter(date_from > ymd("1776-01-01")) %>%
  group_by(from_lab)  %>%
  summarise(from_ratio_sum = sum(ratio)) %>%
  mutate(from_lab = as.factor(from_lab)  %>% fct_reorder(from_ratio_sum))




# POST-CIVIL WAR
postwar_df <- allyears_df %>%
  filter(date_to >= ymd("1861-04-12"))  %>%
  group_by(from_lab)  %>%
  summarise(from_ratio_sum = sum(ratio))  %>%
  mutate(from_lab = as.factor(from_lab)  %>% fct_reorder(from_ratio_sum))


#' # PRE-WAR PLOT
# PRE WAR PLOT ---------------

prewar_plot <- prewar_df  %>%
  filter(from_ratio_sum > 0.05)  %>%
  ggplot(aes(
  x =  from_lab,
  y = from_ratio_sum,
  label = round(from_ratio_sum, 2))
  ) +
  geom_point(size = 2) +
  #geom_text(hjust = -0.1) +
  geom_segment(aes(y = 0,
                   yend = from_ratio_sum,
                   xend = from_lab),
               size = 1.0,
               color = "black") +
  xlab("Source Constitution") +
  ylab("Influence\n(Sum of proportion borrowed)") +
  theme_sc() +
  ggtitle(NULL) +
  theme(
    axis.text.y = element_text(size = rel(.9)),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.y = element_blank()
  ) +
  coord_flip()

graphics.off()
windows(3, 7)
prewar_plot

#' ## DISPLAY TOTAL INFLUENCE
prewar_df  %>% select(from_lab, from_ratio_sum)   %>% print(n = 1000)



# x range
x_lims <- ggplot_build(prewar_plot)$layout$panel_params[[1]]$x.range




#' # POST-WAR PLOT
# POST WAR PLOT ---------------


postwar_plot <- postwar_df  %>%
  filter(from_ratio_sum > 0.05)  %>%
  ggplot(aes(
  x =  from_lab,
  y = from_ratio_sum,
  label = round(from_ratio_sum, 2))
) +
  geom_point(size = 2) +
  #geom_text(hjust = -0.1) +
  geom_segment(aes(y = 0,
                   yend = from_ratio_sum,
                   xend = from_lab),
               size = 1.0,
               color = "black") +
  xlab(NULL) +
  ylab("Influence\n(Sum of proportion borrowed)") +
  theme_sc() +
  ggtitle(NULL) +
  theme(
    axis.text.y = element_text(size = rel(.9)),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.y = element_blank()
  ) +
  coord_flip() +
  ylim(c(0, x_lims[[2]]))

graphics.off()
windows(3, 7)
postwar_plot

#' ## DISPLAY TOTAL INFLUENCE
postwar_df  %>% select(from_lab, from_ratio_sum)   %>% print(n = 1000)






# SAVE ---------------

graphics.off()
windows(3, 7)
prewar_plot


ggsave("Plots/influence_prewar.png")

graphics.off()
windows(3, 7)
postwar_plot
ggsave("Plots/influence_postwar.png")





#  Session Info ------
#' # Display session information:
sessionInfo()
